<template>
  <div class="app-container">
    <el-form
      :model="ruleFormP"
      status-icon
      :rules="rulesP"
      ref="ruleFormP"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleFormP.pass"
          autocomplete="off"
          placeholder="请输入您所要修改的密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleFormP.checkPass"
          autocomplete="off"
          placeholder="请确认您的密码"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleFormP')"
          >设置</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleFormP.checkPass !== "") {
          this.$refs.ruleFormP.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleFormP.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleFormP: {
        pass: "",
        checkPass: ""
      },
      rulesP: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
      this.$store.commit("PersonModule/changePersonP", this.ruleFormP);
      this.$router.go(-1);
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>
